package com.portlet.paypal_bucket;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletRequest;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.payment.validation.CardValidation;
import com.slayer.model.PaypalBucket;
import com.slayer.model.impl.PaypalBucketImpl;
import com.slayer.service.PaypalBucketLocalServiceUtil;

public class PaymentBucketPortlet extends MVCPortlet {

	public void saveAccountDetails(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, SystemException, PortalException {
		
		long bucketId=0l;
		boolean updateAll = true;
		ArrayList<Long> bucketIdList = new ArrayList<Long>();
		List<PaypalBucket> updateAllEntries = new ArrayList<PaypalBucket>();
		
		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
		List<PaypalBucket> paypalBucket = PaypalBucketLocalServiceUtil.getByCompanyId(themeDisplay.getCompanyId());
		
		int[] accountListIndexes = StringUtil.split(
				ParamUtil.getString(actionRequest, "bucketListIndexes"), 0);
		
		for (int i = 1; i <= accountListIndexes.length; i++) {
			int accListIndex = accountListIndexes[i-1];
			
			bucketId = ParamUtil.getLong(actionRequest, "bucketId"+ accListIndex);
			
			String title = ParamUtil.getString(actionRequest, "title"
					+ accListIndex);
			
			String apiUserName = ParamUtil.getString(actionRequest,
					"apiUserName" + accListIndex);
			
			String apiPassword = ParamUtil.getString(actionRequest,
					"apiPassword" + accListIndex);
			
			String apiSignature = ParamUtil.getString(actionRequest,
					"apiSignature" + accListIndex);
			
			String description = ParamUtil.getString(actionRequest,
					"description" + accListIndex);
			
			int defaultEntry = ParamUtil.getInteger(actionRequest,
					"defaultEntry");
			
			if( !CardValidation.isValidInfo(actionRequest,title,apiUserName,apiPassword,apiSignature) ){
				updateAll = false;
				break;
			}
			
			PaypalBucket buck = updateItem(i,accListIndex, actionRequest,paypalBucket,themeDisplay,bucketId,title,apiUserName,apiPassword,apiSignature,description,defaultEntry);
			updateAllEntries.add(buck);
			bucketIdList.add(buck.getBucketId());
		}
		
		if (updateAll) {
			updateAll(updateAllEntries, bucketIdList, accountListIndexes,
					paypalBucket);
		}

	}

	private PaypalBucket updateItem(int i,int index, PortletRequest actionRequest,List<PaypalBucket> paypalBucket,ThemeDisplay themeDisplay,
			long bucketId,String title,String apiUserName,String apiPassword,String apiSignature,String description,int defaultEntry) throws SystemException, PortalException {

		PaypalBucket bucket = null;
	
		long companyId = themeDisplay.getCompanyId();

			if (paypalBucket == null || paypalBucket.isEmpty()) {
				List<PaypalBucket> newList = new ArrayList<PaypalBucket>();
				newList.add(new PaypalBucketImpl());
				paypalBucket = newList;	
			}
			if (bucketId == 0l) {
					bucketId = CounterLocalServiceUtil.increment();
					bucket = PaypalBucketLocalServiceUtil.createPaypalBucket(bucketId);
					bucket.setDateAdded(new Date());
			}else{
					bucket =PaypalBucketLocalServiceUtil.getPaypalBucket(bucketId);
					bucket.setDateModified(new Date());
			}
					bucket.setBucketId(bucketId);
					bucket.setCompanyId(companyId);
					bucket.setTitle(title);
					bucket.setApiUserName(apiUserName);
					bucket.setApiPassword(apiPassword);
					bucket.setApiSignature(apiSignature);
					bucket.setDateModified(new Date());
					bucket.setDescription(description);
					bucket.setDefaultEntry(i == defaultEntry );
				
				return bucket;
	}
	
	private void updateAll(List<PaypalBucket> updateAllEntries,ArrayList<Long> bucketIdList,int[] accountListIndexes, List<PaypalBucket> paypalBucket){
		
		for(PaypalBucket bucket : updateAllEntries)
			try {
				PaypalBucketLocalServiceUtil.updatePaypalBucket(bucket);
				
				if (accountListIndexes.length == bucketIdList.size()) {
					removeItem(bucketIdList,paypalBucket);
				}
				
			} catch (SystemException e) {
				e.printStackTrace();
			}
	}
	
	private void removeItem(ArrayList<Long> bucketIdList,List<PaypalBucket> paypalBuckets) throws SystemException  {
	
		for (PaypalBucket bucket:paypalBuckets){
			if (paypalBuckets.size()!=1 && !bucketIdList.contains(bucket.getBucketId())) {
				PaypalBucketLocalServiceUtil.deletePaypalBucket(bucket);
			}
		}
	}
}
